package team.zero.lemon.module.workflow.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import team.zero.lemon.common.authz.annotation.AuthPassport;
import team.zero.lemon.common.persistence.Page;
import team.zero.lemon.common.web.BaseController;
import team.zero.lemon.module.workflow.entity.Enum.RunState;
import team.zero.lemon.module.workflow.entity.Instance;
import team.zero.lemon.module.workflow.entity.Parameter.*;
import team.zero.lemon.module.workflow.entity.NextActivity;
import team.zero.lemon.module.workflow.entity.Workitem;
import team.zero.lemon.module.workflow.service.*;

import java.util.List;

/**
 * Created by bolin on 2016/10/19.
 */
@Controller
@ResponseBody
@AuthPassport
@RequestMapping(value = "/workflow", method = {RequestMethod.POST, RequestMethod.GET})
public class WorkflowController extends BaseController {
    @Autowired
    private WorkflowService workflowService;
    @Autowired
    private InstanceService instanceService;
    @Autowired
    private ActivityService activityService;

    @Autowired
    private WorkitemService workitemService;

    @AuthPassport
    @RequestMapping("/create")
    public Workitem submit(@RequestBody Create create) {
        Workitem result = workflowService.create(create);
        return result;
    }

    @AuthPassport
    @RequestMapping("/submit")
    public boolean submit(@RequestBody Submit submit) {
        boolean result = workflowService.submit(submit);
        return result;
    }

    @AuthPassport
    @RequestMapping("/activity/next")
    public NextActivity findNextActivitys(@RequestBody Next next) {
        NextActivity result = activityService.findNextActivitys(next.getActivityId(), next.getOrgId(), next.getExpression());
        return result;
    }

    @AuthPassport
    @RequestMapping("/workitem/todo")
    public Page<Workitem> findTodo(@RequestBody Workitem workitem) {
        return workitemService.findTodo(workitem);
    }

    @AuthPassport
    @RequestMapping("/instance/find/create")
    public Page<Instance> findMyCreateWorkflowPage(@RequestBody Instance instance) {
        return instanceService.findMyCreateWorkflowPage(instance);
    }

    @AuthPassport
    @RequestMapping("/instance/find/involved")
    public Page<Instance> findMyInvolvedWorkflowPage(@RequestBody Instance instance) {
        return instanceService.findMyInvolvedWorkflowPage(instance);
    }

    @AuthPassport
    @RequestMapping("/instance/find/monitor")
    public Page<Instance> findMonitorWorkflowPage(@RequestBody Instance instance) {
        return instanceService.findMonitorWorkflowPage(instance);
    }

    @AuthPassport
    @RequestMapping("/instance/update/state")
    public boolean changeInstanceState(@RequestBody Instance instance) {
        return workflowService.changeInstanceState(instance.getId(), RunState.values()[instance.getRunState()]);
    }

}
